# -*- coding: utf-8 -*-
from scpy.logger import get_logger
import os
import sys
from util.pgutil import PgUtil
import requests
from bs4 import BeautifulSoup
from util.category_dic import category_dic
reload(sys)
sys.setdefaultencoding('utf-8')

logger = get_logger(__file__)

CURRENT_PATH = os.path.dirname(__file__)
if CURRENT_PATH:
    CURRENT_PATH = CURRENT_PATH + "/"


pg = PgUtil()

def add_missing_info():

    company_name = u'腾讯科技（深圳）有限公司'
    sql = """
          select url from recruitment where company_name=%s
          """
    value = (company_name,)
    result = pg.select_all_sql(sql,value)
    for item in result:
        _url = item[0]
        soup = BeautifulSoup(requests.get(_url).content)
        try:
            address = ''
            # result['address'] = soup.find('dl', attrs={'class': 'job_company'}).findChildren('div')[-2].getText()
            address_text = soup.find('div',attrs={'class': 'work_addr'}).getText().split('\n')
            salary_text = soup.find('dd',attrs={'class':'job_request'}).span.text
            for item in address_text[0:-2]:
                address += item.strip()
            _sql = """
                    update recruitment set address=%s,salary_text=%s where url=%s
                   """
            _value = (address,salary_text,_url)
            pg.execute_update_sql(_sql,_value)
        except:
            pass


def create_category_info_table():
    for category_type in category_dic.keys():
        for category in category_dic.get(category_type).keys():
            sql = """
                  insert into category_info (category_type,category) values (%s,%s)
                  """
            values = (category_type,category)
            try:
                pg.execute_insert_sql(sql,values)
            except Exception,e:
                logger.error('insert category info failed for %s'%str(e))


if __name__ == '__main__':
    add_missing_info()